package edu.moravian.UI;

import edu.moravian.entity.Enemy;
import edu.moravian.entity.Ordinance;
import edu.moravian.entity.Tower;
import edu.moravian.grid.Grid;
import edu.moravian.grid.GridCell;
import edu.moravian.managers.EnemyManager;
import edu.moravian.managers.OrdinanceManager;
import edu.moravian.managers.TowerManager;
import edu.moravian.math.Point2D;
import edu.moravian.path.Path;
import edu.moravian.player.PlayerInfo;
import edu.moravian.sound.SoundManager;
import edu.moravian.world.World2D;
import edu.moravian.world.WorldDescriptor;
import java.awt.Color;
import java.io.File;

/**
 *
 * @author ronaghan
 */
public final class PlayAreaView extends GameView
{

    private OrdinanceManager ordinanceManager;
    private TowerManager towerManager;
    private EnemyManager enemyManager;
    
    private Grid grid;
    
    private Tower towerToPlace;
    private Tower selectedTower;
    
    private Path path;
    
    private static File placeSound;
    
    private Point2D curMouseLocation;
    
    private TowerUpgradeView towerUpgradeView;
    
    
    
    public PlayAreaView(OrdinanceManager ordinanceManager, TowerManager towerManager, EnemyManager enemyManager, WorldDescriptor boundsDescriptor, Grid grid, 
            TowerUpgradeView towerUpgradeView)
    {
        super(boundsDescriptor);
        this.ordinanceManager = ordinanceManager;
        this.towerManager = towerManager;
        this.enemyManager = enemyManager;
        this.grid = grid;
        
        this.towerUpgradeView = towerUpgradeView;
        
        this.curMouseLocation = null;
        this.towerToPlace = null;
        this.selectedTower = null;
        
        initSoundFiles();
    }
    
    public void removeEntity(Tower tower)
    {
        towerManager.removeTower(tower);
       
        setSelectedtower(null);
        GridCell cell = grid.getCell(tower.getLocation().getX(), tower.getLocation().getY());
        
        cell.setEntity(null);
    }
        
    public synchronized void setCurrTower(Tower tower)
    {
        
        selectedTower = null;
        this.towerUpgradeView.setCurUpgradeTower(null);
        this.towerToPlace = tower;
        
    }
    
    public synchronized void setSelectedtower(Tower tower)
    {
        this.selectedTower = tower;
        this.towerUpgradeView.setCurUpgradeTower(tower);
        this.towerToPlace = null;
    }
            
    
    
    public synchronized void setPath(Path path)
    {
        this.path = path;
    }
    
    public synchronized void draw(World2D w2d)
    {
        
                
        grid.draw(w2d);
        
        for(Enemy enemy : enemyManager.getEnemies())
        {
           w2d.drawRotatedCenteredImage(ImageManager.getEnemy(enemy.getType()), enemy.getLocation(), enemy.getHeading(), enemy.getRadius());

        }

        for(Tower tower : towerManager.getTowers())
        {
            w2d.drawRotatedCenteredImage(ImageManager.getTower(tower.getType()), tower.getLocation(), tower.getHeading(), tower.getRadius());
        }

        w2d.setColor(Color.YELLOW);
        for(Ordinance ordinance : ordinanceManager.getOrdinances())
        {
            w2d.drawCenteredCircle(ordinance.getLocation(), ordinance.getRadius());
        }
        

            if(towerToPlace != null && curMouseLocation != null)
            {

                GridCell curCell = grid.getCell(curMouseLocation.getX(), curMouseLocation.getY());

                Point2D displayPoint = curCell.getCenter();

                if(curCell.hasEntity())
                {            
                    w2d.setColor(Color.RED);
                }
                else
                {
                    w2d.setColor(Color.GREEN);
                }

                w2d.setOpacity(0.5f);

                w2d.drawCenteredCircle(displayPoint, towerToPlace.getFireRadius());


                w2d.setOpacity(1.0f);
                w2d.drawRotatedCenteredImage(ImageManager.getTower(towerToPlace.getType()), displayPoint, towerToPlace.getHeading(), towerToPlace.getRadius());
                w2d.setOpacity(1.0f);
            }
            else if (selectedTower != null)
            {
                w2d.setColor(Color.WHITE);

                w2d.setOpacity(0.5f);

                w2d.drawCenteredCircle(selectedTower.getLocation(), selectedTower.getFireRadius());


                w2d.setOpacity(1.0f);
                w2d.drawRotatedCenteredImage(ImageManager.getTower(selectedTower.getType()), selectedTower.getLocation(), selectedTower.getHeading(), selectedTower.getRadius());
            }
        
        
    }
       
        
        
        
        
    

    public synchronized void handleMouseClick(Point2D location)
    {
       
        GridCell cellClicked = grid.getCell(location.getX(), location.getY());
        if (cellClicked.hasEntity() == false)
        {
            if (towerToPlace != null)
            {
                towerToPlace.getLocation().set(cellClicked.getCenter());
                towerManager.addTower(towerToPlace);
                cellClicked.setEntity(towerToPlace);
                PlayerInfo.setMonies(PlayerInfo.getMonies() - towerToPlace.getTowerProps().getCost());
                towerToPlace = null;
                SoundManager.playFromFile(placeSound);
            }
            else
            {
                setSelectedtower(null);
            }

        }
        else 
        {
            if(cellClicked.getEntity() instanceof Tower)
            {
                setSelectedtower((Tower)cellClicked.getEntity());
            }
        }
        
    }
    
    public  void handleMouseMoved(Point2D location)
    {
        curMouseLocation = location;
    }
    
    private void initSoundFiles()
    {
        placeSound = new File("src/main/resources/sounds/blips/blip2.wav");
    }
}
